package com.tongren.framework.school;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tongren.framework.R;
import com.tongren.framework.utils.play.NewVodListAdapter;
import com.tongren.framework.utils.play.wkvideoplayer.view.MediaController;
import com.tongren.framework.utils.play.wkvideoplayer.view.SuperVideoPlayer;
import com.tongren.framework.utils.play.wkvideoplayer.view.VodRspData;

public class QQPlayerActivity extends Activity implements View.OnClickListener{

    private SuperVideoPlayer mSuperVideoPlayer;
    private ImageView mPlayBtnView;
    private LinearLayout mLyTop;
    private NewVodListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qqplayer);
        initView();
    }

    private void initView() {
        mLyTop = (LinearLayout) findViewById(R.id.layout_top);
        // 创建播放器
        mSuperVideoPlayer = (SuperVideoPlayer) findViewById(R.id.video_player_item_1);
        mSuperVideoPlayer.setVideoPlayCallback(mVideoPlayCallback);

        mPlayBtnView = (ImageView) findViewById(R.id.play_btn);
        mPlayBtnView.setOnClickListener(this);

        mPlayBtnView.setVisibility(View.GONE);
        mSuperVideoPlayer.setVisibility(View.VISIBLE);
        mSuperVideoPlayer.setAutoHideController(false);

        playVideoWithUrl("http://player.alicdn.com/video/aliyunmedia.mp4");
        mSuperVideoPlayer.updateUI("sttt");
    }

    private void playVideoWithUrl(String url) {
        if (mSuperVideoPlayer != null) {
            mSuperVideoPlayer.setPlayUrl(url);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mSuperVideoPlayer.loadVideo();
                }
            });
        }
    }

    private SuperVideoPlayer.VideoPlayCallbackImpl mVideoPlayCallback = new SuperVideoPlayer.VideoPlayCallbackImpl() {
        @Override
        public void onCloseVideo() {
            mSuperVideoPlayer.onDestroy();
            mPlayBtnView.setVisibility(View.VISIBLE);
            mSuperVideoPlayer.setVisibility(View.GONE);
            resetPageToPortrait();
        }

        @Override
        public void onSwitchPageType() {
            if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                mLyTop.setVisibility(View.VISIBLE);
                mSuperVideoPlayer.setPageType(MediaController.PageType.SHRINK);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                mLyTop.setVisibility(View.GONE);
                mSuperVideoPlayer.setPageType(MediaController.PageType.EXPAND);
            }
        }

        @Override
        public void onPlayFinish() {
            mPlayBtnView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onBack() {
            if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                mLyTop.setVisibility(View.VISIBLE);
                mSuperVideoPlayer.setPageType(MediaController.PageType.SHRINK);
            } else {
                finish();
            }
        }

        @Override
        public void onLoadVideoInfo(VodRspData data) {
            mAdapter.addVideoInfo(data);
        }
    };

    /***
     * 恢复屏幕至竖屏
     */
    private void resetPageToPortrait() {
        if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            mSuperVideoPlayer.setPageType(MediaController.PageType.SHRINK);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mSuperVideoPlayer != null) {
            mSuperVideoPlayer.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mSuperVideoPlayer != null) {
            mSuperVideoPlayer.onPause();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.play_btn:
//                if (mCurrentFileIDParam != null){
//                    playVideoWithFileId(mCurrentFileIDParam);
//                }
                break;

        }
    }
}
